package com.xyz.base.action;

import net.sf.json.JSONObject;

/**
 * @author xyzjh
 * 
 */
public class PageAction extends BaseAction {

	/**
	 * 序列号
	 */
	private static final long serialVersionUID = 3909778405948438108L;
	/**
	 * 当前页面
	 */
	private int pageNow = 1;
	
	private int rowNow;

	/**
	 * 每页显示的记录数
	 */
	private int pageSize = 10;

	/**
	 * 总共页数
	 */
	private int pageCount;

	/**
	 * 总记录数
	 */
	private int rowCount;

	/**
	 * 分页内容
	 */
	private String pageContent;
	/**
	 * json格式结果
	 */
	private JSONObject result;
	
	private int rows;
	private int page;

	/**
	 * 返回分页后的总页数
	 * 
	 */
	public int getpageCount() {
		int pagecount = 0;
		if (rowCount % pageSize == 0) {
			pagecount = rowCount / pageSize;
		} else {
			pagecount = rowCount / pageSize + 1;
		}
		pageCount = pagecount;
		return pagecount;
	}

	/**
	 * 返回最后一页的起始记录位置
	 */
	public int lastPageOffset() {
		return rowCount - lastPageSize();
	}

	/**
	 * 返回最后一页的记录数
	 */
	public int lastPageSize() {
		int lastpagesize = 0;
		if (rowCount % pageSize == 0) {
			lastpagesize = pageSize;
		} else {
			lastpagesize = rowCount % pageSize;
		}
		return lastpagesize;
	}

	/**
	 * 返回页面
	 * 
	 * @return
	 */
	public String list() {
		return SUCCESS;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public JSONObject getResult() {
		return result;
	}

	public void setResult(JSONObject result) {
		this.result = result;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public String getPageContent() {
		StringBuffer sBuffer = new StringBuffer(32);
		sBuffer.append("<a href=\"javascript:page(1,3)\" title=\"首页\">&laquo; 首页</a><a href=\"javascript:page(-1,-1)\"	 title=\"前一页\">&laquo; 前一页</a> ");
		for (int i = 1; i < getpageCount()+1; i++) {
			sBuffer.append("<a href=\"javascript:page(");
			sBuffer.append(i);
			sBuffer.append(",3)\"	 class=\"number");
			if (i == pageNow) {
				sBuffer.append(" current");
			}
			sBuffer.append("\" title=\"");
			sBuffer.append(i);
			sBuffer.append("\">");
			sBuffer.append(i);
			sBuffer.append("&nbsp;</a>");
		}
		sBuffer.append("<a href=\"javascript:page(1,1)\" title=\"后一页\">后一页 &raquo;</a><a href='javascript:page(");
		sBuffer.append(pageCount);
		sBuffer.append(",3)'	title=\"末页\">末页 &raquo;</a>");
		pageContent = sBuffer.toString();
		return pageContent;
	}

	public void setPageContent(String pageContent) {

		this.pageContent = pageContent;
	}

	protected void baseDeal() {
		if (pageNow<1) {
			pageNow=1;
		}
		if (pageNow>getpageCount()) {
			pageNow=getpageCount()==0?1:getpageCount();
		}
	}

	public int getRowNow() {
		return rowNow;
	}

	public void setRowNow(int rowNow) {
		this.rowNow = rowNow;
	}
	
}
